<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item   label="学年学期：">
        <el-select class="selectWidth" v-model="dataForm.studyId" placeholder="" >
          <el-option
            v-for="item in studyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称：">
        <el-input class="selectWidth-sm" v-model="dataForm.userName" placeholder="用户名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户电话：">
        <el-input class="selectWidth-sm" v-model="dataForm.userPhone" placeholder="用户电话" clearable></el-input>
      </el-form-item>
      <el-form-item   label="累计学时：">
        <el-select class="selectWidth-sm" @change="changeType" v-model="dataForm.type" placeholder="累计学时" >
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

        <el-select v-if="dataForm.type!==''" class="selectWidth-sm" v-model="dataForm.credit" placeholder="累计学时" >
          <el-option
            v-for="item in typeItemOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="exportDataList()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      ref="tableRef"
      :header-cell-style="tableHeaderStyle"
      :data="dataList" :height="height"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="userPhone"
        header-align="center"
        align="center"
        width="120"
        label="用户电话">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="left"
        align="left"
        label="用户姓名">
      </el-table-column>
      <el-table-column
        prop="lastStudyTime"
        header-align="center"
        align="center"
        width="170"
        label="学习时间">
      </el-table-column>
      <el-table-column
        prop="progress"
        header-align="center"
        align="center"
        width="100"
        label="学习进度">
        <template slot-scope="scope">
          {{scope.row.xuexi}}/{{scope.row.progress}}
        </template>
      </el-table-column>
      <el-table-column
        prop="examTime"
        header-align="center"
        align="center"
        width="170"
        label="考核时间">
      </el-table-column>
      <el-table-column
        prop="progress"
        header-align="center"
        align="center"
        width="100"
        label="考核进度">
        <template slot-scope="scope">
          {{scope.row.cou}}/{{scope.row.progress}}
        </template>
      </el-table-column>

      <el-table-column
        prop="comcou"
        header-align="center"
        align="center"
        width="100"
        label="评价数量">
      </el-table-column>

      <el-table-column
        prop="progressTime"
        header-align="center"
        align="center"
        width="100"
        label="累计学时">
      </el-table-column>


      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="addOrUpdateHandle(scope.row)">详情</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <plan-detail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></plan-detail>
  </div>
</template>

<script>
  import PlanDetail from './plan-detail.vue'
  import { saveAs } from 'file-saver';
  export default {
    data () {
      return {
        tableHeaderStyle: { background: '#f5f7fa !important', height: '40px !important', padding: '4px !important' },
        height: 0,
        dataForm: {
          studyId: '',
          userName: '',
          userPhone: '',
          type: '',
          credit:""
        },
        studyOptions:[

        ],
        typeOptions:[
          {
            value: '',
            label: '请选择'
          },{
            value: 0,
            label: '专业科目'
          }, {
            value: 1,
            label: '公需科目'
          }
        ],
        typeItemOptions:[

        ],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      PlanDetail
    },
    activated () {
      this.getstudyOptionsList()

      this.getDataList()
    },
    mounted() {
      console.log(document.body.clientHeight)
      // 至于减去多少，这个要看你的页面高度和你想将滚动轴固定在哪里了，数值可以更换
      this.$nextTick(() => {
        let tableHeight = document.body.clientHeight - 275
        this.height = tableHeight
      })
    },
    methods: {
      changeType(){
        console.log(this.dataForm.type)
        if(this.dataForm.type == 1){
          this.typeItemOptions = [
            { label: '30及以上', value: 3 },
            { label: '30以下', value: 4 },
          ]
        }else{
          this.typeItemOptions = [
            { label: '60及以上', value: 1 },
            { label: '60以下', value: 2 },
          ]
        }
        this.dataForm.credit = this.typeItemOptions[0].value

      },
      repairTable() {
        this.$nextTick(() => {
          this.$refs.tableRef.doLayout()
        })
      },
      //获取学期学年数据
      getstudyOptionsList () {
        this.$http({
          url: this.$http.adornUrl('/class/study/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 9999999
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            if (data.page.list&&data.page.list.length>0){
              for (let i = 0; i < data.page.list.length; i++) {
                this.studyOptions.push({
                  value: data.page.list[i].id,
                  label: data.page.list[i].name
                })
              }
              //默认选中第一个
              this.dataForm.studyId = this.studyOptions[0].value
            }
          }
        })
      },
      exportDataList () {
        let searchParams = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'studyId': this.dataForm.studyId,
          'key': this.dataForm.userName,
          'userPhone': this.dataForm.userPhone,
          'type': this.dataForm.type
        }
        if( this.dataListSelections.length != 0){
          const ids =  this.dataListSelections.map(item => item.userId).join(',');
          searchParams.ids = ids;
        }
        this.$http.exportFile('/class/manage/exportStudyUserList',  searchParams)
          .then(async (res)  => {
            console.log(res);
            let blob = new Blob([res])
            saveAs(blob, "学习进度.xlsx")
          }).catch(error => { console.error('文件导出失败:', error) })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let searchParams = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'studyId': this.dataForm.studyId,
          'key': this.dataForm.userName,
          'userPhone': this.dataForm.userPhone,
          'type': this.dataForm.type,
          // 使用模板字符串：
          [ `credit${this.dataForm.credit}` ]: this.dataForm.credit,
        }
        this.$http({
          url: this.$http.adornUrl('/class/manage/studyUserList'),
          method: 'get',
          params: this.$http.adornParams(searchParams)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
          this.repairTable()

        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        console.log(val)
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (item) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(item)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/class/study/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
<style >
/*el-table__row--level-1 下的 el-table__cell下的 el-table__indent padding设置为0*/

</style>
